home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / TOOLPAS2 / PREPHELP.PAS < prev    next >
Pascal/Delphi Source File  |  1989-02-28  |  6KB  |  268 lines

  1.  
  2. (*
  3.  * PrepHelp - Prepare Help File
  4.  *            Supporting utility for Helpme unit.
  5.  *
  6.  * Written by S.H.Smith, 3-14-89
  7.  * Copyright 1989 Samuel H. Smith
  8.  * Copyright 1989 Omnicraft, Inc.
  9.  *
  10.  *)
  11.  
  12. {$i prodef.inc}
  13. {$m 8000,0,0}
  14.  
  15. uses Helpme;
  16.  
  17. var
  18.    infd:    text;
  19.    ofd:     text;
  20.    obuf:    array[1..20000] of byte;
  21.  
  22.    key:     array[1..maxkey] of keyword_rec;
  23.    keys:    integer;
  24.  
  25.    line:    string;
  26.    loc:     word;
  27.  
  28. var
  29.    sub:     array[1..maxsub] of string[keylen];
  30.    subkey:  array[1..maxsub] of byte;
  31.    subs:    integer;
  32.  
  33.  
  34. (* ---------------------------------------------------------- *)
  35. procedure parse_sub;
  36. var
  37.    p: integer;
  38. begin
  39.    while copy(line,1,1) = ' ' do
  40.       delete(line,1,1);
  41.    p := pos(' ',line);
  42.    if p = 0 then
  43.       p := length(line)+1;
  44.  
  45.    if subs < maxsub then
  46.       inc(subs);
  47.  
  48.    sub[subs] := copy(line,1,p-1);
  49.    line := copy(line,p+1,255);
  50.  
  51.    for p := 1 to length(sub[subs]) do
  52.       if sub[subs][p] = '_' then
  53.          sub[subs][p] := ' ';
  54. end;
  55.  
  56.  
  57.  
  58. (* ---------------------------------------------------------- *)
  59. procedure build_keywords;
  60. var
  61.    maxlen:  integer;
  62.    nlines:  integer;
  63.  
  64. begin
  65.    keys := 0;
  66.    loc := 3;
  67.    while not eof(infd) do
  68.    begin
  69.       readln(infd,line);
  70.  
  71.       if copy(line,1,2) = '--' then    {define keyword}
  72.       begin
  73.          if keys > 0 then
  74.          begin
  75.             key[keys].width := maxlen;
  76.             key[keys].lines := nlines;
  77.             inc(loc,9);
  78.          end;
  79.  
  80.          nlines := 0;
  81.          line := copy(line,3,255);
  82.          subs := 0;
  83.          parse_sub;
  84.  
  85.          inc(keys);
  86.          key[keys].id := sub[1];
  87.          key[keys].loc := loc;
  88.  
  89.          maxlen := length(sub[1])+2;
  90.          if maxlen < 13 then
  91.             maxlen := 13;
  92.          subs := 0;
  93.          while line <> '' do
  94.          begin
  95.             parse_sub;
  96.             inc(maxlen,3+length(sub[subs]));
  97.          end;
  98.  
  99.       end
  100.       else
  101.  
  102.       if copy(line,1,1) <> ';' then
  103.       begin                            {plain text}
  104.          inc(nlines);
  105.          if length(line) > maxlen then
  106.             maxlen := length(line);
  107.          inc(loc,length(line)+1);
  108.       end;
  109.    end;
  110.  
  111.    if keys > 0 then
  112.    begin
  113.       key[keys].width := maxlen;
  114.       key[keys].lines := nlines;
  115.       inc(loc,9);
  116.    end;
  117. end;
  118.  
  119.  
  120. (* ---------------------------------------------------------- *)
  121. procedure generate_keytab;
  122. var
  123.    i:       integer;
  124.    ocol:    integer;
  125.  
  126.    procedure put(c: byte);
  127.    begin
  128.       if ocol > 72 then
  129.       begin
  130.          writeln(ofd);
  131.          write(ofd,'      ');
  132.          ocol := 6;
  133.       end;
  134.       write(ofd,c:3,',');
  135.       inc(ocol,4);
  136.    end;
  137.  
  138. begin
  139.  
  140.    writeln(ofd);
  141.    writeln(ofd,'{Generated from ',paramstr(1),' by PrepHelp}');
  142.    writeln(ofd);
  143.  
  144.    writeln(ofd,'const');
  145.    writeln(ofd,'   keyword_count = ',keys,';');
  146.    writeln(ofd);
  147.  
  148.    writeln(ofd,'   keyword_table: array[1..keyword_count] of keyword_rec = (');
  149.    for i := 1 to keys do
  150.    begin
  151.       write(ofd,'      {',i:2,
  152.                   '}  (loc: ',key[i].loc:5,
  153.                   ';  width: ',key[i].width:2,
  154.                   ';  lines: ',key[i].lines:2,
  155.                   ';  id: ''',key[i].id,''')');
  156.  
  157.       if i = keys then
  158.          writeln(ofd,' );')
  159.       else
  160.          writeln(ofd,',');
  161.    end;
  162.  
  163.    writeln(ofd);
  164.    write(ofd,'   help_text: array[1..',loc-1,'] of byte = (');
  165.    ocol := 99;
  166.  
  167.    reset(infd);
  168.    while not eof(infd) do
  169.    begin
  170.       readln(infd,line);
  171.  
  172.       if copy(line,1,2) = '--' then    {refer to keyword}
  173.       begin
  174.          line := copy(line,3,255);
  175.          subs := 0;
  176.          parse_sub;
  177.  
  178.          ocol := 99;
  179.          put(1); put(0);     {#0 = end of topic}
  180.          write(ofd,'  {',subkey[1],' - ',sub[1],'}');
  181.  
  182.          subs := 0;
  183.          while line <> '' do
  184.          begin
  185.             parse_sub;
  186.             subkey[subs] := 0;
  187.             for i := 1 to keys do
  188.                if sub[subs] = key[i].id then
  189.                   subkey[subs] := i;
  190.             if subkey[subs] = 0 then
  191.             begin
  192.                writeln('unknown subtopic(',subs,'): ',sub[subs]);
  193.             end;
  194.          end;
  195.  
  196.          ocol := 99;
  197.          for i := 1 to maxsub do
  198.             if i > subs then
  199.                put(0)
  200.             else
  201.                put(subkey[i]);
  202.  
  203.          ocol := 99;
  204.       end
  205.       else
  206.  
  207.       if copy(line,1,1) <> ';' then
  208.       begin                            {plain text}
  209.          inc(loc,length(line)+1);
  210.          put(length(line));
  211.          for i := 1 to length(line) do
  212.             put(ord(line[i]));
  213.       end;
  214.    end;
  215.  
  216.    ocol := 99;
  217.    put(1);
  218.    writeln(ofd,'  0 );');
  219.    writeln(ofd);
  220.    writeln(ofd,'procedure help(x,y: integer; key: string);');
  221.    writeln(ofd,'var');
  222.    writeln(ofd,'   id,p: integer;');
  223.    writeln(ofd,'begin');
  224.    writeln(ofd,'   id := 1;');
  225.    writeln(ofd,'   for p := 1 to keyword_count do');
  226.    writeln(ofd,'      if keyword_table[p].id = key then');
  227.    writeln(ofd,'         id := p;');
  228.    writeln(ofd,'   help_on_tap(1,x,y,id,keyword_table,help_text); ');
  229.    writeln(ofd,'end;');
  230.    writeln(ofd);
  231.  
  232. end;
  233.  
  234.  
  235. (* ---------------------------------------------------------- *)
  236. begin
  237.    if paramcount <> 2 then
  238.    begin
  239.       writeln('usage: prephelp infile outfile');
  240.       halt(99);
  241.    end;
  242.  
  243.    assign(infd,paramstr(1));
  244.    {$i-} reset(infd); {$i+}
  245.    if ioresult <> 0 then
  246.    begin
  247.       writeln('can''t open helpfile: ',paramstr(1));
  248.       halt(99);
  249.    end;
  250.  
  251.    build_keywords;
  252.  
  253.    assign(ofd,paramstr(2));
  254.    {$i-} rewrite(ofd); {$i+}
  255.    if ioresult <> 0 then
  256.    begin
  257.       writeln('can''t create output: ',paramstr(2));
  258.       halt(99);
  259.    end;
  260.  
  261.    setTextBuf(ofd,obuf);
  262.  
  263.    generate_keytab;
  264.  
  265.    close(infd);
  266.    close(ofd);
  267. end.
  268.